<?php

namespace app\store\controller;

use app\store\model\Order as OrderModel;
use app\store\model\Express as ExpressModel;
use app\store\model\store\shop\Clerk as ShopClerkModel;
use app\store\model\store\Shop as ShopModel;
use app\api\model\Order as OrderModel1;
use app\common\model\Wxapp as WxappModel;
use app\common\library\wechat\WxSubMsg;
/**
 * 订单管理
 * Class Order
 * @package app\store\controller
 */
class Order extends Controller
{
    /**
     * 待发货订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function delivery_list()
    {
        return $this->getList('待就诊订单列表', 'receipt_list');
    }

    /**
     * 已完成订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function receipt_list()
    {
        return $this->getList('已完成订单列表', 'receipt');
    }

    /**
     * 待付款订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function pay_list()
    {
        return $this->getList('待付款订单列表', 'pay');
    }

    /**
     * 已完成订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function complete_list()
    {
        return $this->getList('已完成订单列表', 'complete');
    }

    /**
     * 已取消订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function cancel_list()
    {
        return $this->getList('已取消订单列表', 'cancel');
    }

    /**
     * 全部订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function all_list()
    {
        return $this->getListaaa('全部订单列表', 'all');
    }

    /**
     * 订单详情
     * @param $order_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail($order_id)
    {
        // 订单详情
        $detail = OrderModel::detail($order_id);
        $order_goods = DB("order_goods")->where("order_id",$order_id)->find();
        $jiuzhen = DB("user_jiuzhen")->where("jiuzhen_id",$order_goods['jiuzhen_id'])->find();
        // dump($order_goods);die;
        return $this->fetch('detail', compact(
            'detail',
            'order_goods',
            'jiuzhen'
        ));
    }

    /**
     * 确认发货 跳过
     * @param $order_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function delivery($order_id)
    {
        $model = OrderModel::detail($order_id);
        if ($model->delivery($this->postData('order'))) {
            if ($model->order_type == 2) {
                return $this->renderSuccess('发货完成');
            }
            return $this->renderSuccess('到诊完成');
        }
        return $this->renderError($model->getError() ?: '到诊失败');
    }
    /**
     * 确认收货
     * @param $order_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function receipt($order_id)
    {
        $model1 = OrderModel::detail($order_id);
        $model = OrderModel1::getUserOrderDetail($order_id, $model1->user_id);
        if ($model->receipt()) {
            // 开始发送消息
            // 获取小程序配置
            $wxappId = "10001";
            $wxConfig = WxappModel::getWxappCache($wxappId);
            // 请求微信api执行发送
            $WxSubMsg = new WxSubMsg($wxConfig['app_id'], $wxConfig['app_secret']);
            $params = [
                'touser' => DB("user")->where("user_id", $model1['user_id'])->value('open_id'), // 获取用户 open_id
                'template_id' => 'BTPuZSks35Rl0SpyUz8Y-djMrf-evtk50h5aUQ-gBCQ', // 模板ID
                'page' => '', // 跳转的页面路径，可根据需求补充
                'data' => [
                    'name1' => [
                        'value' => DB("user_jiuzhen")
                            ->where("jiuzhen_id", $model1['goods'][0]['jiuzhen_id'])
                            ->value('name') ?: '未填写姓名'
                    ],
                    'name9' => [
                        'value' => $model1['goods'][0]['goods_name'] ?: '未填写商品名称'
                    ],
                    'name2' => [
                        'value' => DB("category")
                            ->where('category_id', DB("goods")->where('goods_id', $model1['goods'][0]['goods_id'])->value('category_id'))
                            ->value('name') ?: '未分类'
                    ],
                    'date4' => [
                        'value' => $model1['goods'][0]['jiuzhen_day'] ?: '无就诊日期'
                    ],
                    'thing8' => [
                        'value' => "您好，您已成功就诊"
                    ],
                ],
            ];
            $WxSubMsg->sendTemplateMessage($params);
            return $this->renderSuccess();
        }
        return $this->renderError($model->getError());
    }
 
    /**
     * 修改订单价格
     * @param $order_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function updatePrice($order_id)
    {
        $model = OrderModel::detail($order_id);
        if ($model->updatePrice($this->postData('order'))) {
            return $this->renderSuccess('修改成功');
        }
        return $this->renderError($model->getError() ?: '修改失败');
    }

    /**
     * 订单列表
     * @param string $title
     * @param string $dataType
     * @return mixed
     * @throws \think\exception\DbException
     */
    private function getList($title, $dataType)
    {
        // 订单列表
        $model = new OrderModel;
        $list = $model->getList($dataType, $this->request->param());
        // 新增就诊人
        // dump($list);die;
        // 自提门店列表
        $shopList = ShopModel::getAllList();
        // dump($list);die;
        return $this->fetch('index', compact('title', 'dataType', 'list', 'shopList'));
    }
    private function getListaaa($title, $dataType)
    {
        // 订单列表
        $model = new OrderModel;
        $list = $model->getListaaa($dataType, $this->request->param());
        // 新增就诊人
        // dump($list->toArray());die;
        // 自提门店列表
        $shopList = ShopModel::getAllList();
        // dump($list);die;
        return $this->fetch('index', compact('title', 'dataType', 'list', 'shopList'));
    }

}
